package main

//todo
import (
	"encoding/json"
	"fmt"
)

type Response1 struct {
	page   int
	fruits []string
}
type Response2 struct {
	page   int      `json:"page"`
	fruits []string `json:"fruits"`
}

func main() {
	bolB, _ := json.Marshal(true)
	fmt.Println(string(bolB))

	intB, _ := json.Marshal(1)
	fmt.Println(string(intB))

	fltB, _ := json.Marshal(4.25)
	fmt.Println(string(fltB))

	strB, _ := json.Marshal("gopher")
	fmt.Println(string(strB))

	//下面是切片和map编码成json数组和对象的例子
	slcD := []string{"apple", "peach", "pear", "banana", "watermelon"}
	slcB, _ := json.Marshal(slcD)
	fmt.Println(string(slcB))

	mapD := map[string]int{"apple": 5, "lettuce": 7}
	mapB, _ := json.Marshal(mapD)
	fmt.Println(string(mapB))

	// json包可以自动的编码你的自定义类型
	//编码仅输出可导出的字段，并且默认使用他们的名字作为json数据的键
	res1D := &Response1{
		page:   1,
		fruits: []string{"apple", "peach", "pear"}}
	res1B, _ := json.Marshal(res1D)
	fmt.Println(string(res1B))

	res2D := &Response2{
		page:   1,
		fruits: []string{"apple", "peach", "pear"}}
	res2B, _ := json.Marshal(res2D)
	fmt.Println(string(res2B))

}
